<template>
  <div id="app">

    <router-view :curCity="curCity"/>
    <div class="loading-box" v-if="show">
      <img src="./assets/images/loading_2.png" alt="">
    </div>
    <!--文本提示弹窗-->
    <TextPop v-if="isPop" :textPop="popText"></TextPop>
  </div>
</template>

<script>
  import global from './components/public/global.vue'

  export default {
    name: 'App',
    data() {
      return {
        locationData: {},
        isPop: false,//是否弹出提示文本框
        popText: '',//弹出提示文本内容
        curCity: "",
        show:true,
        num:0,
      }
    },

    methods: {
      bindRelation(uid){
        if(uid){
          let params = new URLSearchParams();
          params.append("uid", uid);
          let url = global.apiUrl;
          this.$http({
            url: url + '/user/bindRelation',
            method: 'post',
            data: params,
          }).then(res => {
            console.log(res);
            if (res.data.data == 0) {
              window.localStorage.removeItem("shareId")
            } else {

            }
          }).catch(function (err) {
            console.error(err);
          });
        }
      }
    },
    beforeCreate() {
      var uid = this.$route.query.uid;
      if (uid != undefined) {
        window.localStorage.setItem("shareId",uid);
      }
        //      let curCity = sessionStorage.getItem('curCity');
        let curCity = this.$store.getters.getCity;

        if (!curCity) {
//        sessionStorage.setItem('curCity', '北京市');
          this.$store.dispatch('setCity','北京市');
          //获取当前城市
//      getLocation() {
          let self = this;
          let map, geolocation;
          //加载地图，调用浏览器定位服务
          map = new AMap.Map('container', {
            resizeEnable: true
          });
          map.plugin('AMap.Geolocation', function () {
            geolocation = new AMap.Geolocation({
              enableHighAccuracy: true,//是否使用高精度定位，默认:true
              timeout: 10000,          //超过10秒后停止定位，默认：无穷大
              buttonOffset: new AMap.Pixel(10, 20),//定位按钮与设置的停靠位置的偏移量，默认：Pixel(10, 20)
              zoomToAccuracy: true,      //定位成功后调整地图视野范围使定位位置及精度范围视野内可见，默认：false
              buttonPosition: 'RB'
            });
            map.addControl(geolocation);
            geolocation.getCurrentPosition();
            AMap.event.addListener(geolocation, 'complete', onComplete);//返回定位信息
            AMap.event.addListener(geolocation, 'error', onError);      //返回定位出错信息
          });

          //解析定位结果
          function onComplete(data) {
            console.log(data);
            self.locationData = data;
            let lat = data.position.lat;
            let lng = data.position.lng;
            let latlng=[];
            latlng.push(lat);
            latlng.push(lng);
            let tempCity = data.addressComponent.city;
            let addressComponent=data.addressComponent.province+data.addressComponent.city+data.addressComponent.district
            self.$store.dispatch('setCity',tempCity);
            self.$store.dispatch('setLatLng',latlng);
            self.$store.dispatch('setAddressComponent',addressComponent);
            latlng=[];
            latlng.push(lng);
            latlng.push(lat);
            localStorage.setItem("latlng",latlng)
            self.isPop = true;
            self.popText = '定位成功';
            self.num++;
            if(self.num>=1){
              setTimeout(() => {
                self.show=false;
              }, 300)
            }
            setTimeout(() => {
              self.isPop = false;
            }, 1000)
          }

          //解析定位错误信息
          function onError(data) {
            self.isPop = true;
            self.popText = '定位失败';
            setTimeout(() => {
              self.isPop = false;
            }, 1000)
            self.num++;
            if(self.num>=1){
              setTimeout(() => {
                self.show=false;
              }, 300)
            }
          }
          //循环定位开始

          setTimeout(function () {
              let level=global.getCookie("level");
              if(level>=3){
                loop();
              }

          }, 10000 )

          function loop(){
            map.plugin('AMap.Geolocation', function () {
              geolocation = new AMap.Geolocation({
                enableHighAccuracy: true,//是否使用高精度定位，默认:true
                //timeout: 10000,          //超过10秒后停止定位，默认：无穷大
                buttonOffset: new AMap.Pixel(10, 20),//定位按钮与设置的停靠位置的偏移量，默认：Pixel(10, 20)
                zoomToAccuracy: true,      //定位成功后调整地图视野范围使定位位置及精度范围视野内可见，默认：false
                buttonPosition: 'RB'
              });
              map.addControl(geolocation);
              geolocation.getCurrentPosition();
              AMap.event.addListener(geolocation, 'complete', loopComplete);//返回定位信息
              AMap.event.addListener(geolocation, 'error', loopError);      //返回定位出错信息
            });


          }
          function loopComplete(data) {
            console.log(data);
            let locat=data.position;
            let addr=data.addressComponent;
            self.$http.get(global.apiUrl + '/user/uploadLocation?longitude='+locat.lng+"&latitude="+locat.lat+"&province="+addr.province+"&city="+addr.city+"&area="+addr.district+"&addr="+data.formattedAddress).then(res => {

            })
            setTimeout(function () {
              loop();
            }, 1000*60 )
          }
          //解析定位错误信息
          function loopError(data) {
            console.log(data);
            setTimeout(function () {
              loop();
            }, 1000*60 )
          }



//      },
        }
    },
    mounted() {
      let self=this;
      var uid = this.$route.query.uid;
      if (uid != undefined) {
        window.localStorage.setItem("shareId",uid);
      }
      if(window.localStorage.getItem("shareId")!=null){
        global.bindRelation(window.localStorage.getItem("shareId"),this)
      }
      var openid = this.$route.query.openId;
      var from1 = this.$route.query.from;
//      if(uid!=undefined||from1!=undefined){
//        window.location = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxb2e099eb45c56187&redirect_uri=http://www.yingla9.com/yl/user/checkLogin&response_type=code&scope=snsapi_userinfo&state=aaa&connect_redirect=1#wechat_redirect";
//        return;
//      }

        self.$http.get(global.apiUrl + "/user/checkLogin").then((res) => {

          if (res.data == 0) {
            setTimeout(function () {
            window.location = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxb2e099eb45c56187&redirect_uri=http://www.yingla9.com/yl/user/checkLogin&response_type=code&scope=snsapi_userinfo&state=aaa&connect_redirect=1#wechat_redirect";
            }, 200)
          }else if(res.data==1){
            window.location.href="http://www.yingla9.com/dist/#/";
          }
          else{
            global.setCookie("username", res.data.nickname);
            global.setCookie("uid", res.data.id);
            global.setCookie("level", res.data.level);
            window.localStorage.setItem("uid",res.data.id);
            window.localStorage.setItem("headImg", res.data.headImg);
            window.localStorage.setItem("username", res.data.nickname);
            window.localStorage.setItem("mobile", res.data.mobile);
            window.localStorage.setItem("name", res.data.name);
            if(window.localStorage.getItem("shareId")!=null){
              global.bindRelation(window.localStorage.getItem("shareId"),self)
            }


            if(ul.indexOf("openId")>=1){
              window.location.href="http://www.yingla9.com/dist/#/";
            }

//            self.num++;
//            if(self.num>=2){
//              self.show=false;
//            }


            let text ="http://www.yingla9.com/dist/#/?uid="+window.localStorage.getItem("uid");
          }
        });



      if (openid == undefined) {




      } else {
        console.log("openid"+openid)
        window.location.href="http://www.yingla9.com/dist/#/";
        self.$http.get(global.apiUrl + "/user/text?openId=" + openid).then((res) => {
          global.setCookie("username", res.data.nickname);
          global.setCookie("uid", res.data.id);
          window.localStorage.setItem("uid",res.data.id);
          window.localStorage.setItem("headImg", res.data.headImg);
          window.localStorage.setItem("username", res.data.nickname);
          window.localStorage.setItem("mobile", res.data.mobile);
          window.localStorage.setItem("name", res.data.name);
          global.setCookie("level", res.data.level);
          self.num++;
          if(self.num>=2){
            self.show=false;
          }
          let text ="http://www.yingla9.com/dist/#/?uid="+window.localStorage.getItem("uid");
        });
      }
      if(window.localStorage.getItem("shareId")!=null){
        global.bindRelation(window.localStorage.getItem("shareId"),self)
      }

    }

  }


</script>

<style lang="less">
  html, body {
    font-family: Microsoft YaHei UI;
    font-size: 18px;
    width: 100%;
    height: 100%;
  }

  a {
    text-decoration: none;
    color: #333;
  }

  div, ul, li, p {
    box-sizing: border-box;
    padding: 0;
    margin: 0;
  }

  input, textarea, button {
    -webkit-appearance: none;
    outline: none;
    vertical-align: top;
    margin: 0;
    padding: 0;
    font-family: Microsoft YaHei UI;
  }

  .clamp2 {
    width: 100%;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    overflow: hidden;
  }

  a {
    text-decoration: none;
    color: #333;
  }

  div, ul, li, p {
    box-sizing: border-box;
    padding: 0;
    margin: 0;
  }

  .clamp2 {
    width: 100%;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    overflow: hidden;
  }
  .loading-box{
    position: fixed;
    top:0;
    left:0;
    width:100%;
    height:100%;
    /*background:rgba(0,0,0,.2);*/
    z-index: 1000;
    img{
      position: absolute;
      top:40%;
      left:0;
      right:0;
      margin:auto;
      width:50px;
      height:50px;
      animation: rotat 1s linear infinite;
    }
  }
  @keyframes rotat {
    0%{transform: rotate(0deg);}
    100%{transform:rotate(360deg);}
  }

</style>
